<template>
  <div id="shopList">
    <!--主盒子-->
    <div class="container">
      <!--左侧栏-->
      <div class="nav_left">
        <van-sidebar v-model="activeKey" style="width: 100%">
          <van-sidebar-item
            v-for="(item, index) in cateItems"
            :key="index"
            :title="item.classifyName"
            @click="onChange(item)"
          />
        </van-sidebar>
      </div>
      <!--右侧栏-->
      <div class="nav_right">
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
          <van-swipe-item
            v-for="(item, index) in bannerImg"
            :key="index"
            class="bannerImg"
          >
            <img :src="item.adPicture" @click="bannerImgToWhere(item)" alt="" />
          </van-swipe-item>
        </van-swipe>
        <!--如果有数据，才遍历项-->
        <div v-if="showNull">
          <div
            class="nav_right_items"
            v-for="(item, index) in shopList"
            :key="index"
            @click="goOtherPage(item)"
          >
            <van-badge :content="item.productLabel">
              <img :src="item.productIcon" alt="" />
            </van-badge>
            <span>{{ item.productName }}</span>
            <span class="fiveSile">{{ item.productDescribe }}</span>
          </div>
        </div>
        <!--如果无数据，则显示数据-->
        <div class="nodata_text" v-else>该分类暂无数据</div>
      </div>
    </div>
  </div>
</template>

<script>
import req from "@/utils/index";
export default {
  name: "shopList",
  data() {
    return {
      cateItems: [],
      curNav: 1,
      curIndex: 0,
      activeKey: 0,
      shopList: {},
      bannerImg: [],
      showNull: true,
    };
  },
  methods: {
    //事件处理函数
    switchRightTab (e) {
      // 获取item项的id，和数组的下标值
      let id = e.target.dataset.id,
        index = parseInt(e.target.dataset.index);
      // 把点击到的某一项，设为当前index
      this.curNav = id;
      this.curIndex = index;
    },
    // 更改时，获取不同分类的商品
    onChange(item) {
      this.getShopList(item.id);
    },
    // 获取不同分类的商品
    getShopList(id) {
      req("get", `/qyHome/getProduct?classifyId=${id}`, {}).then((res) => {
        if (res.rspCode == "000000") {
          this.bannerImg = res.data.adList;
          this.shopList = res.data.classifyProduct;
          if (this.shopList.length == 0) {
            this.showNull = false;
          } else {
            this.showNull = true;
          }
        }
      });
    },
    // 跳转至详情页面
    goOtherPage(item) {
      // console.log(item)
      if (item.productUrl.substring(0, 4) == "http") {
        window.location.href = item.productUrl + `&tsToken=${window.localStorage.getItem('logintoken')}`;
      }
    },
    // 轮播图跳转
    bannerImgToWhere(item) {
      if (item.adUrl.substring(0, 4) == "http") {
        window.location.href = item.adUrl + `&tsToken=${window.localStorage.getItem('logintoken')}`;
      } else {
        this.$router.push({
          name: "creatOrder",
          query: {
            goodsNo: item.adUrl,
            didPay: "noYet",
            isGsq: false,
          },
        });
      }
    },
  },
  created() {
    this.shopList = this.cateItems[0];
    req("get", "/qyHome/getClassify", {}).then((res) => {
      if (res.rspCode == "000000") {
        this.cateItems = res.data.list;
        this.getShopList(res.data.list[0].id);
      }
    });
  },
  watch: {},
};
</script>

<style lang='scss' scoped>
/*总体主盒子*/
.container {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #fff;
  color: #939393;
}
/*左侧栏主盒子*/
.nav_left {
  /*设置行内块级元素（没使用定位）*/
  display: inline-block;
  width: 30%;
  height: 100%;
  /*主盒子设置背景色为灰色*/
  background: #f5f5f5;
  text-align: center;
}
/*左侧栏list的item*/
.nav_left .nav_left_items {
  /*每个高30px*/
  height: 40px;
  /*垂直居中*/
  line-height: 40px;
  /*再设上下padding增加高度，总高42px*/
  padding: 6px 0;
  /*只设下边线*/
  border-bottom: 1px solid #dedede;
  /*文字14px*/
  font-size: 14px;
}
/*左侧栏list的item被选中时*/
.nav_left .nav_left_items.active {
  /*背景色变成白色*/
  background: #fff;
  color: #f0145a;
}
/*右侧栏主盒子*/
.nav_right {
  /*右侧盒子使用了绝对定位*/
  position: absolute;
  top: 0;
  right: 0;
  flex: 1;
  /*宽度75%，高度占满，并使用百分比布局*/
  width: 70%;
  height: 1000px;
  padding: 10px;
  box-sizing: border-box;
  background: #fff;
}
/*右侧栏list的item*/
.nav_right .nav_right_items {
  /*浮动向左*/
  float: left;
  /*每个item设置宽度是33.33%*/
  width: 33.33%;
  height: 2.3rem;
  text-align: center;
  margin-top: 0.4rem;
}
.van-badge__wrapper {
  width: 52%;
  /deep/.van-badge.van-badge--fixed {
    right: -0.2rem;
    border-radius: 100px 100px 100px 0px;
  }
}
.nav_right .nav_right_items img {
  /*被图片设置宽高*/
  // width: 60px;
  // height: 60px;
  width: 100%;
}
.nav_right .nav_right_items span {
  /*给text设成块级元素*/
  display: block;
  font-size: 14px;
  color: #333;
  /*设置文字溢出部分为...*/
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.nodata_text {
  color: black;
  font-size: 14px;
  text-align: center;
}
.bannerImg {
  width: 100%;
  img {
    width: 100%;
    height: 2rem;
  }
}
.nav_right .nav_right_items span.fiveSile {
  font-size: 0.25rem;
  color: #999999;
}


/deep/.van-sidebar-item--select, .van-sidebar-item--select:active {
  background: none;
  ::before {
    left: .3rem;
  }
  .van-sidebar-item__text {
    width: 130%;
    background-color: #fff;
    padding: .3rem 0;
    border-radius: .5rem 0 0 .5rem;
    margin-left: -.2rem;
  }
}
/deep/.van-sidebar-item--select::before {
  left: .3rem;
  z-index: 999;
}
</style>
